
let CardQueue = require('CardQueue');
let CardDeal = require('CardDeal');
let CardHold = require('CardHold');


cc.Class({
    extends: cc.Component,

    properties: {
        cardQueues: [CardQueue],
        cardDealArea: CardDeal,
        cardHoldArea: CardHold,

        scoreLabel: cc.Label
    },

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, (event) => {

        });

        this.cardDealArea.node.on('change-top', () => {
            if ( this.checkFailed() ) {
                this.gameOver();
            }
        });


        this.score = 0;
        for (let cardQueue of this.cardQueues) {
            cardQueue.node.on('match_card', this.onCardMatched.bind(this));
        }

        this.eraseMode = false;

    },

    update(dt) {

    },

    onTouchStart(event) {
        let pos = event.getLocation();

        if (this.eraseMode) {
            for (let cardQueue of this.cardQueues) {
                if ( cardQueue.containsPoint(pos) ) {
                    this.eraseCardQueue = cardQueue;
                }
            }
            return;
        }


        let card = this.cardDealArea.getTopCard();
        // cc.log('--TS:', card.node.getBoundingBoxToWorld(), event.getLocation());
        if ( card.containsPoint(pos) ) {
            this.touchCard = card;
            card.startPos = card.position;
            return;
        }

        card = this.cardHoldArea.card;
        if (card && card.containsPoint(pos)) {
            this.touchCard = card;
            card.startPos = card.position;
            return;
        }
    },
    onTouchMove(event) {
        let touchCard = this.touchCard;
        if (touchCard) {
            touchCard.x += event.getDeltaX();
            touchCard.y += event.getDeltaY();
        }
    },
    onTouchEnd(event) {
        let pos = event.getLocation();

        if (this.eraseMode) {
            let eraseCardQueue = this.eraseCardQueue;
            if ( eraseCardQueue && eraseCardQueue.containsPoint(pos) ) {
                let cardIdx = eraseCardQueue.findCardIndexByPos(pos);
                if (cardIdx >= 0)
                    eraseCardQueue.eraseCard(cardIdx);
            }
            return;
        }


        let touchCard = this.touchCard;
        if (touchCard) {
            for (let cardQueue of this.cardQueues) {
                if ( cardQueue.containsPoint(pos) ) {
                    if ( cardQueue.isFull() && cardQueue.getLastCard().num != touchCard.num ) {
                        break;
                    }

                    let card;
                    if (touchCard.isHold) {
                        card = this.cardHoldArea.removeCard();
                    } else {
                        card = this.cardDealArea.shiftCard();
                    }
                    cardQueue.addCard(card);

                    this.touchCard = null;
                    return;
                }
            }

            if ( !touchCard.isHold
                && this.cardHoldArea.isEmpty()
                && this.cardHoldArea.containsPoint(pos) ) {
                let card = this.cardDealArea.shiftCard();
                this.cardHoldArea.setCard(card);
            }

            touchCard.position = touchCard.startPos;
            this.touchCard = null;
        }
    },

    onCardMatched() {
        this.score += 10;

        this.scoreLabel.string = this.score;
    },

    tapItem(event) {
        let buttonNode = event.target;

        // let itemNode = new cc.Node();
        // this.node.addChild(itemNode);
        //
        // let itemSp = itemNode.addComponent(cc.Sprite);
        // itemSp.spriteFrame = button.getComponent(cc.Sprite).spriteFrame;

        if (this.eraseMode) {
            buttonNode.stopAllActions();
            buttonNode.scale = 1;
            buttonNode.rotation = 0;

            this.eraseMode = false;
        } else {
            buttonNode.rotation = -30;
            buttonNode.runAction( cc.repeatForever( cc.sequence( cc.scaleTo(0.5, 1.1), cc.scaleTo(0.5, 0.9) ) ) );

            this.eraseMode = true;
        }
    },

    checkFailed() {
        for (let cardQueue of this.cardQueues) {
            if ( !cardQueue.isFull() ) return false;

            let card = cardQueue.getLastCard();
            if (card.num == this.cardDealArea.getTopCard().num) {
                return false;
            }
        }

        return true;
    },

    gameOver() {
        cc.log('<--GAME-OVER-->');
    }
});
